package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import school.domain.Recharge;
import school.domain.User;
import school.mapper.RechargeMapper;
import school.mapper.TcMapper;
import school.mapper.UserMapper;
import school.support.BaseController;
import school.support.pojo.RechargePo;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@RequestMapping("recharge")
@Controller
public class RechargeController extends BaseController {

    @Autowired
    private RechargeMapper rechargeMapper;
    @Autowired
    private TcMapper tcMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 列表
     *
     * @param recharge
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(RechargePo recharge, @RequestParam(defaultValue = "1") Integer pageNum, Model model,
                       String starttime,String endtime) {
        Example example = new Example(Recharge.class);
        Example.Criteria criteria = example.createCriteria();
        if (recharge.getName() != null) {
            criteria.andCondition("name like '%" + recharge.getName() + "%'");
        }
        if (!sessionUser().getPhone().equals("admin")) {
            criteria.andCondition("u.id = ", sessionUser().getId());
        }
        if(!StringUtil.isEmpty(starttime)){
            criteria.andCondition("r.addtime >= ",starttime);
        }
        if(!StringUtil.isEmpty(endtime)){
            criteria.andCondition("r.addtime <= ",endtime);
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<RechargePo> pageInfo = new PageInfo<>(rechargeMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "recharge/list";
    }


    /**
     * 流量充值
     * @return
     */
    @GetMapping("ll")
    public String ll(Model model){
        model.addAttribute("type","流量");
        return "recharge/add";
    }

    @GetMapping("hf")
    public String hf(Model model){
        model.addAttribute("type","话费");
        return "recharge/add";
    }

    @PostMapping("add")
    public String add(Recharge recharge){
        User user = userMapper.selectByPrimaryKey(sessionUser().getId());
        if(recharge.getType().equals("话费")) user.setAccount(user.getAccount() + recharge.getAccount());
        else user.setTraffic(user.getTraffic() + recharge.getAccount());
        userMapper.updateByPrimaryKeySelective(user);
        recharge.setAddtime(new Date());
        recharge.setUserid(user.getId());
        rechargeMapper.insertSelective(recharge);
        return "redirect:list";
    }
}
